<?php
/**
 * 视频模型
 * @DateTime 2017-09-18
 * @author xyl
*/
class Video_model extends MY_Model
{
	private $table = 'video';

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 动态获取视频列表
	 * @DateTime 2017-09-18
 	 * @author xyl
	 * @param  string $limit [每页显示数量]
	 * @param  string $start [开始位置]
	 * @param  string $info  [信息]
	 * @return [array]        [list:数据主体 count:数据总数]
	 */
	public function ajaxGetVideoList($limit, $start, $info = false)
	{
		$where['status'] = 1;
		$order = 'type asc,create_time desc';
		if(isset($info['type']) && $info['type']!='') $where['type'] = $info['type'];
		if(isset($info['name'])){
			$like = array(
				'theme'	=>	$info['name'],
			);
			$data = $this->hw_getList_like_limit($this->table,$where,'*',$like,$limit,$start);
			if(count($data)>0){
				$total = $this->db->where($where)->like($like,'both')->from($this->table)->count_all_results();
			}else{
				$total = 0;
			}
			return array('data'=>$data,'total'=>$total);
		}
		if(isset($info['start_time']) && !empty($info['start_time'])) $where['create_time >='] = $info['start_time'];
		if(isset($info['end_time']) && !empty($info['end_time'])) $where['create_time <='] = $info['end_time'];

		$data = $this->db->from($this->table)->where($where)->limit($limit,$start)->order_by($order)->get()->result_array();
		if(count($data)>0){
			$total = $this->db->from($this->table)->where($where)->count_all_results();
		}else{
			$total = 0;
		}
		return array('data'=>$data,'total'=>$total);
	}

	/**
	 * 获取首页展示的视频列表
	 * @DateTime 2017-09-018
 	 * @author xyl
	 * @return [type] [description]
	 */
	public function getVidelList()
	{
		$where['status'] = 1;
		$where['type'] = 1;
		$list = $this->hw_getList($this->table,$where);
		$link_arr = array_column($list,'link');
		return $link_arr;
	}

	/**
	 * 获取还可设置的视频的数量
	 * @DateTime 2017-09-018
 	 * @author xyl
	 * @return [type] [description]
	 */
	public function getVideosnum()
	{
		$where['status'] = 1;
		$where['type'] = 1;
		$num = $this->db->from($this->table)->where($where)->count_all_results();
		$left_num = 10-$num;
		return $left_num;
	}

	/**
	 * 获取单条视频详情
	 * @DateTime 2017-09-18
 	 * @author xyl
	 * @param  [type] $where   [查询条件]
	 * @return [type]          [description]
	 */
	public function getVideoDetail($where)
	{
		$info = $this->hw_getRecord($this->table,$where);
		return $info;
	}

	/**
	 * 添加或更新视频
	 * @DateTime 2017-09-18
 	 * @author xyl
	 * @param [type] $post [description]
	 */
	public function addVideo($post)
	{
		$data = array(
			'theme'			=>	$post['theme'],
			'default_img' 	=>	$post['default_img'],
			'link' 			=>	$post['link'],
		);
		if (isset($post['id']) && !empty($post['id'])) {
			$data['update_time'] = date('Y-m-d H:i:s');
			$where['id'] = $post['id'];
			$before = $this->getVideoDetail($where);
			$log_details = array('before'=>$before,'after'=>$data);
			$res = $this->hw_Update($this->table,$data,$where);
			$msg = '编辑';
			$type = 2;
			$id = $post['id'];
		} else {
			$res = $this->hw_Insert($this->table,$data);
			$msg = '添加';
			$type = 1;
			$log_details = $data;
			$id = $res;
		}
		if ($res) {
			$result = array('code'=>1, 'msg'=>$msg.'视频成功');
		} else {
			$result = array('code'=>0, 'msg'=>$msg.'视频失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($result['code'],$type,$id,$this->table,'视频',json_encode($log_details));
		return $result;
	}

	/**
	 * 设置上线和下架
	 * @DateTime 2017-09-18
     * @author xyl
	 * @param [type] $post [description]
	 */
	public function setRoll($post)
	{
		$ids = $post['id'];
		$data['update_time'] = date('Y-m-d H:i:s');
		$data['type'] = $post['roll_type'];
		$log_details = array();
		$edit_ids = array();
		$num = $this->getVideosnum();

		foreach ($ids as $value) {
			$before = $this->hw_getRecord($this->table,array('id'=>$value));
			if($before['type']!=$data['type']){
				$edit_ids[] = $value;
				$log_details[] = array('before'=>$before,'after'=>$data);
			}    
		}
		if ($data['type']==1) {
			if (count($edit_ids)>$num) {
				return array('code'=>0,'msg'=>'操作失败，设置播放的视频不能超过10个');
			}
		}

		$this->db->trans_begin();
		if(!empty($edit_ids) && count($edit_ids)>0){
			for ($i=0; $i < count($edit_ids) ;$i++) { 
				$this->hw_Update($this->table,$data,array('id'=>$edit_ids[$i]));
			}	
		}
		
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
			$result = array('code'=>0,'msg'=>'操作失败');
		}
		else
		{
			$this->db->trans_commit();
			$result = array('code'=>1,'msg'=>'操作成功');
		}
		if(!empty($edit_ids) && count($edit_ids)>0){
			for ($i=0; $i < count($edit_ids) ;$i++) { 
				$detail = $log_details[$i];
				$this->logMdl->addOperationLog($result['code'],2,$edit_ids[$i],$this->table,'视频状态',json_encode($detail));
			}	
		}
		return $result;
	}
	
}